
import java.awt.*;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import javax.swing.JPanel;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author Figueroa
 */
public class Board extends JPanel{
     // width of the chessboard square
   int width;
   // number of black pixels surrounding each square
   int border=3;
   // color of chess squares
   Color lightSquare= Color.white;// new Color(122,122,122);  // light green
   Color darkSquare= new Color(127,127,127);//new Color(234,234,234);     // dark green
   
   //boolean whiteTurn=true;;
   
   // used when dragging pieces from one square to another
   boolean dragging=false;
   boolean temp;
   int tempRow, tempCol;
   int dragRow, dragCol;
   char  dragPiece;
   //Unicode Pieces
   //White set
   private String pawnW  = "\u2659";
   private String kingW  = "\u2654";
   private String queenW = "\u2655";
   private String rookW  = "\u2656";
   private String bishopW = "\u2657";
   private String knightW = "\u2658";
 
   //Black set
   private String pawnB  = "\u265F";
   private String kingB  = "\u265A";
   private String queenB = "\u265B";
   private String rookB  = "\u265C";
   private String bishopB = "\u265D";
   private String knightB = "\u265E";
  public char startposition[] = {'R','H','B','Q','K','B','H','R',
         'P','P','P','P','P','P','P','P',
         ' ',' ',' ',' ',' ',' ',' ',' ',
         ' ',' ',' ',' ',' ',' ',' ',' ',
         ' ',' ',' ',' ',' ',' ',' ',' ',
         ' ',' ',' ',' ',' ',' ',' ',' ',
         'p','p','p','p','p','p','p','p',
         'r','h','b','q','k','b','h','r'};


   
     // remember the location of all pieces on board
   public char piece[] =
        {'R','H','B','Q','K','B','H','R',
         'P','P','P','P','P','P','P','P',
         ' ',' ',' ',' ',' ',' ',' ',' ',
         ' ',' ',' ',' ',' ',' ',' ',' ',
         ' ',' ',' ',' ',' ',' ',' ',' ',
         ' ',' ',' ',' ',' ',' ',' ',' ',
         'p','p','p','p','p','p','p','p',
         'r','h','b','q','k','b','h','r'};
  
  // isWhite -- true if piece is a white chesspiece
  public boolean isWhite(char ch) {
     return (ch>='a' && ch<='z');
  }
  
  public boolean legalMove(int i, int j) {
     // global variables:
     //    subject piece:  dragPiece
     //    from position:  dragCol, dragRow 
     if (i<0 || i>7 || j<0 || j>7) {
        return false;
     }
     // do not allow move to position occupied by 
     // player making the move
     if (piece[j*8+i]!=' ') {
        // '^' is XOR: false if same,  true if different
        //return isWhite(dragPiece)^isWhite(piece[j*8+i]);
     return true;
     }
     // for now move anywhere
     return true;
  }
  
  private void labelSquare(Graphics g, int i, int j, String piece) {
    /* Polygon poly=new Polygon();
     for (int ip=0; ip<xpoints.length; ip++) {
        poly.addPoint(pos(i)+xpoints[ip]*width/31,
                      pos(j)+ypoints[ip]*width/31);
      }
      g.fillPolygon(poly);*/
      g.setColor(Color.BLACK);
    //  g.drawPolygon(poly);
      int fontsize = width ;
      g.setFont(new Font("TimesRoman", Font.PLAIN,fontsize));
     // System.out.print("I"+pos(i)+"J"+pos(j));
      g.drawString(piece,pos(i),pos(j)+fontsize-border-3);
  }
  
  // 
  public boolean isDark(int i,int j) {
     // ^ is exclusive or -- true if both operands are the same
     // test: both i and j are even or both i and j are odd
     return ((i&1)^(j&1))!=0;
  }
  
  // determine position of row i or column i
  public int pos(int i) {
     return border+i*(width+border);
  }
  
  // position inverse --
  // determine row i or column i from position
  public int posinv(int p) {
     // we solve for i in pos() function 
     return (p-border)/(width+border);
  }
  
  public void drawSquare(Graphics g, int i, int j, boolean red) {
     String[] array = new String[6];
      if (red) {
        g.setColor(new Color(50,205,50));
     } else {
        if (isDark(i,j)) {
           g.setColor(darkSquare);
        } else {
           g.setColor(lightSquare); 
        } 
     }
    
     g.fillRect(pos(i),pos(j),width,width);
     if (isWhite(piece[j*8+i])) {
         
       array[0] = pawnW;
       array[1] = kingW;
       array[2] = queenW;
       array[3] = rookW;
       array[4] = knightW;
       array[5] = bishopW;
     } else {
       array[0] = pawnB;
       array[1] = kingB;
       array[2] = queenB;
       array[3] = rookB;
       array[4] = knightB;
       array[5] = bishopB;
     }
     switch (Character.toUpperCase(piece[j*8+i])) {
     case 'P': labelSquare(g,i,j,array[0]);
               break;
     case 'K': labelSquare(g,i,j,array[1]);
               break;
     case 'Q': labelSquare(g,i,j,array[2]);
               break;
     case 'R': labelSquare(g,i,j,array[3]);
               break;
     case 'H': labelSquare(g,i,j,array[4]);
               break;
     case 'B': labelSquare(g,i,j,array[5]);
               break;
     default:  break;
     }
  }
  
  private void drawBoard(Graphics g) {
     g.setColor(Color.black);
     g.fillRect(0,0,width*8+border*9,width*8+border*9);
     for (int i=0;i<8;i++) {
        for (int j=0;j<8;j++) {
           drawSquare(g,i,j,false);
        }
     
     }
  }
  
    // Paint does all the work -- draw the chessboard
   public void paint(Graphics g) {
     Dimension d=size();
      width=(Math.min(d.width,d.height)-border*9)/8;
      drawBoard(g); 
   }

 

   public void update(Graphics g) {
      paint(g);
   }
   
   public char get_piece(String p){
   
       if(p.equals(pawnW))
           return 'p';
     else if(p.equals(kingW))
           return 'k';
     else if(p.equals(queenW))
           return 'q';
     else if(p.equals(rookW))
           return 'r';   
     else if(p.equals(knightW))
           return 'h';
     else if(p.equals(bishopW))
           return 'b';
       
      else  if(p.equals(pawnB))
           return 'P';
     else if(p.equals(kingB))
           return 'K';
     else if(p.equals(queenB))
           return 'Q';
     else if(p.equals(rookB))
           return 'R';   
     else if(p.equals(knightB))
           return 'H';
     else if(p.equals(bishopB))
           return 'B';
     else
         return ' ';
      
       
   
   } 
   public String get_piece_name(String p){
   
       if(p.equals(pawnW))
           return "Peón Blanco";
     else if(p.equals(kingW))
           return "Rey Blanco";
     else if(p.equals(queenW))
           return "Reina Blanca";
     else if(p.equals(rookW))
           return "Torre Blanca";   
     else if(p.equals(knightW))
           return "Caballo Blanco";
     else if(p.equals(bishopW))
           return "Alfil Blanco";
       
      else  if(p.equals(pawnB))
           return "Peón Negro";
     else if(p.equals(kingB))
           return "Rey Negro";
     else if(p.equals(queenB))
           return "Reina Negra";
     else if(p.equals(rookB))
           return "Torre Negra";   
     else if(p.equals(knightB))
           return "Caballo Negro";
     else if(p.equals(bishopB))
           return "Alfil Negro";
     else
         return " ";
      
       
   
   }
   
   public String piece_tostring(){
  
   String resul ="";
   
   for(int i=0;i<piece.length;i++){
   resul+=piece[i];
   }
   
   return resul;
       
       
   }
   
   public void limpiar(){
   Graphics g=this.getGraphics();
   for(int i=0; i<piece.length;i++){
   piece[i]=' ';
 
   
   }
     update(g);
   }
  
   

}